package com.videoadmin.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.videoadmin.dao.ICommentsDAO;
import com.videoadmin.pojo.Comments;
import com.videoadmin.utils.HibernateUtil;

public class CommentsDAOImpl implements ICommentsDAO {
	
	/* (non-Javadoc)
	 * @see com.video.dao.ICommentsDao#saveOrUpdate(com.domain.Comments)
	 */
	public void saveOrUpdate(Comments comments){
		Session s = null;
		Transaction ts = null;
		try {
			s = HibernateUtil.getSession();
			ts = s.beginTransaction();
			s.saveOrUpdate(comments);
			ts.commit();
		} catch (HibernateException e) {
			if (ts != null)
				ts.rollback();
			throw e;
		} finally {
			if (s != null)
				s.close();
		}
	}
	/* (non-Javadoc)
	 * @see com.video.dao.ICommentsDao#delete(com.domain.Comments)
	 */
	public void delete(Comments comments){
		Session s = null;
		Transaction ts = null;
		try {
			s = HibernateUtil.getSession();
			ts = s.beginTransaction();
			s.delete(comments);
			ts.commit();
		} catch (HibernateException e) {
			if (ts != null)
				ts.rollback();
			throw e;
		} finally {
			if (s != null)
				s.close();
		}
	}
	/* (non-Javadoc)
	 * @see com.video.dao.ICommentsDao#findByCommentId(int)
	 */
	public Comments findByCommentId(int id){
		Session s = null;
		try {
			s = HibernateUtil.getSession();
			Comments comments = (Comments) s.get(Comments.class, id);
			return comments;
		} finally {
			if (s != null)
				s.close();
		}
	}
	/* (non-Javadoc)
	 * @see com.video.dao.ICommentsDao#findByUserId(int)
	 */
	public List<Comments> findByUserId(int id){
		Session s=null;
		List<Comments> list=new ArrayList<Comments>();
		try{
			s = HibernateUtil.getSession();
			list=(List<Comments>)s.createSQLQuery("select * from Comments where User_Id='"+id+"'").addEntity(Comments.class).list();
			return list;
		}finally {
			if (s != null)
				s.close();
		}
	}
	/* (non-Javadoc)
	 * @see com.video.dao.ICommentsDao#findByMovieId(int)
	 */
	public List<Comments> findByMovieId(int id){
		Session s=null;
		List<Comments> list=new ArrayList<Comments>();
		try{
			s = HibernateUtil.getSession();
			list=(List<Comments>)s.createSQLQuery("select * from Comments where Movie_Id='"+id+"'").addEntity(Comments.class).list();
			return list;
		}finally {
			if (s != null)
				s.close();
		}
	}

}
